﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using WindowsStateService.Common;

namespace WindowsStateService
{
    public static class StateManager
    {
        private static PerformanceCounter cpuCounter;
        static StateManager()
        {
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        }

        public static string GetWindowsStateInfo()
        {
            return "state";
        }

        public static string GetWindowsNewLogs() {
            return "logs";
        }

        /// <summary>
        /// 获取物理内存信息
        /// </summary>
        /// <returns></returns>
        public static PhysicalMemoryDetail GetPhysicalMemoryInfo()
        {
            Microsoft.VisualBasic.Devices.Computer myInfo = new Microsoft.VisualBasic.Devices.Computer();
            PhysicalMemoryDetail detail = new PhysicalMemoryDetail();
            detail.TotalSize = myInfo.Info.TotalPhysicalMemory / 1024 / 1024 / 1024;
            detail.AvailableSize = myInfo.Info.AvailablePhysicalMemory / 1024 / 1024 / 1024;
            detail.UsedSize = (myInfo.Info.TotalPhysicalMemory - myInfo.Info.AvailablePhysicalMemory) / 1024 / 1024 / 1024;
            return detail;
        }

        /// <summary>
        /// 获取磁盘信息
        /// </summary>
        /// <returns></returns>
        public static IList<DriveDetail> GetTotalDriveSize()
        {
            IList<DriveDetail> drives = new List<DriveDetail>();
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.IsReady)
                {
                    DriveDetail detail = new DriveDetail();
                    detail.Name = drive.Name;
                    detail.TotalSize = drive.TotalSize / 1024 / 1024 / 1024;
                    detail.UsedSize = (drive.TotalSize - drive.AvailableFreeSpace) / 1024 / 1024 / 1024;
                    detail.AvailableSize = drive.AvailableFreeSpace / 1024 / 1024 / 1024;
                }
            }
            return drives;
        }

        /// <summary>
        /// cpu 使用率
        /// </summary>
        /// <returns></returns>
        public static float GetCPUValue()
        {
            return cpuCounter.NextValue();
        }

        private static string GetHardwareInfo()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            ManagementObjectCollection collection = searcher.Get();
            return "";
        }


    }
}
